//
//  MBStatisticsExcelView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBStatisticsExcelView.h"
#import <FormScrollView.h>

@interface MBStatisticsExcelView ()<FDelegate,FDataSource>
@property (nonatomic,weak)FormScrollView* topTable;
@property (nonatomic,weak)FormScrollView* bottomTable;

@property (nonatomic,strong) NSArray* topSectionArray;
@property (nonatomic,strong) NSArray* bottomSectionArray;

@property (nonatomic,weak)UIScrollView* scrollView;

@end

@implementation MBStatisticsExcelView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIScrollView* scrollView = [[UIScrollView alloc]init];
        self.scrollView = scrollView;
        [self addSubview:scrollView];
        
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsZero);
        }];
        
        @weakify(self);
        // 添加头部刷新控件
        MJRefreshNormalHeader * activityHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.isRefresh = YES;
        }];
        
        scrollView.mj_header = activityHeader;
        [activityHeader setTitle:@"下拉即可刷新" forState:MJRefreshStateIdle];
        [activityHeader setTitle:@"松开即可更新" forState:MJRefreshStatePulling];
        [activityHeader setTitle:@"加载中" forState:MJRefreshStateRefreshing];
        activityHeader.lastUpdatedTimeLabel.hidden = YES;

        
        UIImageView* dayImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"day_icon"]];
        [scrollView addSubview:dayImageView];
        
        [dayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:18]);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:12]);
        }];
        
        NSDate *now = [NSDate date];
        NSCalendar *cal = [NSCalendar currentCalendar];
        unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *dd = [cal components:unitFlags fromDate:now];
        NSInteger year = [dd year];
        NSInteger month = [dd month];

        
        UIButton* dayButton = [[UIButton alloc]init];
        self.dayButton = dayButton;
        [dayButton setTitle:[NSString stringWithFormat:@"%ld年%ld月",(long)year,(long)month] forState:UIControlStateNormal];
        dayButton.titleLabel.font = [UIFont lf_systemFontOfSize:14];
        [dayButton setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
        [dayButton setImage:[UIImage imageNamed:@"day_choose"] forState:UIControlStateNormal];
        dayButton.imageEdgeInsets = UIEdgeInsetsMake(0, dayButton.titleLabel.intrinsicContentSize.width+[UIView lf_sizeFromIphone6:10], 0, 0);
        dayButton.titleEdgeInsets = UIEdgeInsetsMake(0, -[UIView lf_sizeFromIphone6:10], 0, 0);
        [scrollView addSubview:dayButton];
        
        [dayButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(dayImageView);
            make.left.equalTo(dayImageView.mas_right).offset([UIView lf_sizeFromIphone6:5]);
            make.width.mas_equalTo(dayButton.intrinsicContentSize.width+[UIView lf_sizeFromIphone6:10]);
        }];

        FormScrollView* topTable = [[FormScrollView alloc]initWithFrame:CGRectZero];
        topTable.contentInset = UIEdgeInsetsZero;
        topTable.fDelegate = self;
        topTable.fDataSource = self;
        self.topTable = topTable;
        [scrollView addSubview:topTable];
        
        [topTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(dayImageView.mas_bottom).offset([UIView lf_sizeFromIphone6:14]);
            make.left.equalTo(dayImageView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:24]);
            make.height.mas_offset([UIView lf_sizeFromIphone6:58]);
        }];
        
        UILabel* subTitleLabel = [[UILabel alloc]init];
        subTitleLabel.text = @"明细";
        subTitleLabel.font = [UIFont lf_systemFontOfSize:14];
        subTitleLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [scrollView addSubview:subTitleLabel];
        
        [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(dayImageView);
            make.top.equalTo(topTable.mas_bottom).offset([UIView lf_sizeFromIphone6:20]);
        }];

        FormScrollView* bottomTable = [[FormScrollView alloc]initWithFrame:CGRectZero];
        bottomTable.contentInset = UIEdgeInsetsZero;
        bottomTable.fDelegate = self;
        bottomTable.fDataSource = self;
        self.bottomTable = bottomTable;
        [scrollView addSubview:bottomTable];
        
        [bottomTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(subTitleLabel.mas_bottom).offset([UIView lf_sizeFromIphone6:20]);
            make.left.equalTo(dayImageView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:24]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:340]);
            make.bottom.equalTo(scrollView);
        }];

    }
    return self;
}


-(NSArray *)bottomSectionArray
{
    if (_bottomSectionArray == nil) {
            }
    return _bottomSectionArray;
}

#pragma mark - fDelegate fDataSource
-(NSInteger)numberOfSection:(FormScrollView *)formScrollView
{
    if (formScrollView == self.topTable) {
        return 1;
    }else {
        if (self.model) {
            return self.model.list.count;
        }
        return self.reportModel.list.count;
    }
}

-(NSInteger)numberOfColumn:(FormScrollView *)formScrollView
{
    if (formScrollView == self.topTable) {
        if (self.model) {
            return 3;
        }else {
            return 4;
        }
    }else {
        return 4;
    }
}

- (FTopLeftHeaderView *)topLeftHeadViewForForm:(FormScrollView *)formScrollView {
    return nil;
}


-(CGFloat)heightForSection:(FormScrollView *)formScrollView
{
    return [UIView lf_sizeFromIphone6:26];
}

-(CGFloat)widthForColumn:(FormScrollView *)formScrollView
{
    if (formScrollView == self.topTable) {
        if (self.model) {
            return (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:24])/3;
        }else {
            return (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:24])/4;
        }
    }else {
        return (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:24])/4;
    }
}

- (FormSectionHeaderView *)form:(FormScrollView *)formScrollView sectionHeaderAtSection:(NSInteger)section {
    return nil;
}

- (FormColumnHeaderView *)form:(FormScrollView *)formScrollView columnHeaderAtColumn:(NSInteger)column {
    FormColumnHeaderView *header = [formScrollView dequeueReusableColumnWithIdentifier:@"Column"];
    if (header == NULL) {
        header = [[FormColumnHeaderView alloc] initWithIdentifier:@"Column"];
    }
    header.titleLabel.font = [UIFont lf_mediumFontOfSize:13];
    if (formScrollView == self.topTable) {
        [header setTitle:self.topSectionArray[column] forState:UIControlStateNormal];
    }else {
        [header setTitle:self.bottomSectionArray[column] forState:UIControlStateNormal];
    }
    [header setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
    return header;
}

- (FormCell *)form:(FormScrollView *)formScrollView cellForColumnAtIndexPath:(FIndexPath *)indexPath {
    FormCell *cell = [formScrollView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == NULL) {
        cell = [[FormCell alloc] initWithIdentifier:@"Cell"];
    }
    cell.titleLabel.font = [UIFont lf_mediumFontOfSize:13];
    if (formScrollView == self.topTable) {
        if (indexPath.column == 0) {
            [cell setTitleColor:[UIColor colorWithR:255 G:0 B:0] forState:UIControlStateNormal];
            if (self.model) {
                [cell setTitle:[NSString stringWithFormat:@"%.2f",self.model.today.today_amount] forState:UIControlStateNormal];
            }else {
                [cell setTitle:[NSString stringWithFormat:@"%.2f",self.reportModel.goods.goods_amount] forState:UIControlStateNormal];
            }
        }else {
            [cell setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];

            if (indexPath.column == 1) {
                if (self.model) {
                    [cell setTitle:[NSString stringWithFormat:@"%ld",(long)self.model.today.today_order] forState:UIControlStateNormal];
                }else {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",self.reportModel.goods.coupon_amount] forState:UIControlStateNormal];
                }
            }else if(indexPath.column == 2){
                if (self.model) {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",self.model.today.sign] forState:UIControlStateNormal];
                }else {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",self.reportModel.goods.cost_price] forState:UIControlStateNormal];
                }
            }else {
                [cell setTitle:[NSString stringWithFormat:@"%.2f",self.reportModel.goods.shipping_amount] forState:UIControlStateNormal];
            }
        }
    }else {
        [cell setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
        if (self.model) {
            MBStatisticsProfileDayModel* model = self.model.list[indexPath.section];
            if (indexPath.column == 0) {
                [cell setTitle:model.day forState:UIControlStateNormal];
            }else {
                if (indexPath.column == 1) {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",model.amount] forState:UIControlStateNormal];
                }else if(indexPath.column == 2){
                    [cell setTitle:[NSString stringWithFormat:@"%d",model.order_num] forState:UIControlStateNormal];
                }else {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",model.sign] forState:UIControlStateNormal];
                }
            }
        }else {
            MBStatisticsReportGoodsModel* goodsModel = self.reportModel.list[indexPath.section];
            if (indexPath.column == 0) {
                [cell setTitle:goodsModel.day forState:UIControlStateNormal];
            }else {
                if (indexPath.column == 1) {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",goodsModel.goods_amount] forState:UIControlStateNormal];
                }else if(indexPath.column == 2){
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",goodsModel.coupon_amount] forState:UIControlStateNormal];
                }else {
                    [cell setTitle:[NSString stringWithFormat:@"%.2f",goodsModel.cost_amount] forState:UIControlStateNormal];
                }
            }
        }
    }
    return cell;
}

-(void)setEndRefresh:(BOOL)endRefresh
{
    _endRefresh = endRefresh;
    
    [self.scrollView.mj_header endRefreshing];
}

-(void)setModel:(MBStatisticsProfileModel *)model
{
    _model = model;
    if (!model) {
        return;
    }
    self.topSectionArray = @[@"交易金额",@"订单数",@"客单价"];
    self.bottomSectionArray = @[@"日期",@"交易金额",@"订单数",@"客单价"];

    [self.topTable reloadData];
    [self.bottomTable reloadData];

}

-(void)setReportModel:(MBStatisticsReportModel *)reportModel
{
    _reportModel = reportModel;
    if (!reportModel) {
        return;
    }

    self.topSectionArray = @[@"商品总额",@"优惠金额",@"商品成本",@"净营收"];
    self.bottomSectionArray = @[@"日期",@"商品总额",@"优惠金额",@"商品成品"];

    [self.topTable reloadData];
    [self.bottomTable reloadData];
}

@end
